package com.sihan.swvp.action.sjfx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.service.jc.JcBdzService;
import com.sihan.swvp.service.sjfx.KshzsService;

@Controller
@RequestMapping("sjfx/KshzsAction")
public class KshzsAction extends BaseAction{
	@Autowired
	private JcBdzService jcBdzService;
	@Autowired
	private KshzsService kshzsService;
	
	@ResponseBody
	@RequestMapping("/getVtree") 
	public List<Map<String, Object>> getVtree(@Param("node")String node){
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> erlist = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		map.put("text", "变电站");
		map.put("iconCls", "icon-open");
		map.put("expanded",true);
	
		
		 List<Map<String, Object>> listData=jcBdzService.getJcBdzAll(params);
		 for (int i = 0; i < listData.size(); i++) {
			 Map<String, Object> mapBdz=new HashMap<String,Object>();
			 mapBdz.put("text", listData.get(i).get("text"));
			 mapBdz.put("pid", listData.get(i).get("pid"));
			 mapBdz.put("leaf",true);
			 
			 erlist.add(mapBdz);
		 }	map.put("children", erlist);
		 list.add(map);
		return list;
	}
	
	/**
	 * 任务总数饼图
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getKshzsBingCount") 
	public Object getKshzsBingCount(@Param("jcBdzId")Integer jcBdzId){
		Map<String, Object> param=new HashMap<String,Object>();
		param.put("jcBdzId", jcBdzId);
		Map<String, Object> result=getCountByWorksy(jcBdzId);
		List<Map<String, Object>> resultList=new ArrayList<Map<String, Object>>();
		int todayCount=CommonUtil.o2int(result.get("todayCount"));
		int weekCount=CommonUtil.o2int(result.get("weekCount"));
		int monthCount=CommonUtil.o2int(result.get("monthCount"));
		int monthCountOk=CommonUtil.o2int(result.get("monthCountOk"));
		if(todayCount==0 && weekCount==0 && monthCount==0){
			result.put("name", "无数据");
			resultList.add(result);
			return resultList;
		}else{
			Map<String, Object> resut1=new HashMap();
			Map<String, Object> resut2=new HashMap();
//			result.put("name", "今日");
//			result.put("data", todayCount);
//			resultList.add(result);
			resut2.put("name", "未完成");
			resut2.put("data", monthCount);
			resut1.put("name", "已完成");
			resut1.put("data", monthCountOk);
			resultList.add(resut1);
			resultList.add(resut2);
			return resultList;
		}
	}
	
	/**
	 * 完成率柱状图
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getKshzsZhuZhuangCount") 
	public Object getKshzsZhuZhuangCount(@Param("jcBdzId")Integer jcBdzId){
		Map<String, Object> result=getCountByWorksy(jcBdzId);
		List<Map<String, Object>> resultList=new ArrayList<Map<String, Object>>();
		int todayCount=CommonUtil.o2int(result.get("todayCount"));
		int weekCount=CommonUtil.o2int(result.get("weekCount"));
		int monthCount=CommonUtil.o2int(result.get("monthCount"));
		int todayCountOk=CommonUtil.o2int(result.get("todayCountOk"));
		int weekCountOk=CommonUtil.o2int(result.get("weekCountOk"));
		int monthCountOk=CommonUtil.o2int(result.get("monthCountOk"));
			Map<String, Object> resut1=new HashMap();
			Map<String, Object> resut2=new HashMap();
			result.put("name", "今日");
			result.put("未完成", todayCount);
			result.put("已完成", todayCountOk);
			resultList.add(result);
			resut1.put("name", "本周");
			resut1.put("未完成", weekCount);
			resut1.put("已完成", weekCountOk);
			resultList.add(resut1);
			resut2.put("name", "本月");
			resut2.put("未完成", monthCount);
			resut2.put("已完成", monthCountOk);
			resultList.add(resut2);
		return resultList;
	}
	/**
	 * 统计数量
	 * @param jcBdzId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCountByWorksy") 
	public Map<String, Object> getCountByWorksy(@Param("jcBdzId")Integer jcBdzId){
		Map<String, Object> param=new HashMap();
		param.put("jcBdzId", jcBdzId);
		Map<String, Object> result=kshzsService.getCountByWorksy(param);
		int todayCount=CommonUtil.o2int(result.get("todayCount"));
		int todayCountOk=CommonUtil.o2int(result.get("todayCountOk"));
		int weekCount=CommonUtil.o2int(result.get("weekCount"));
		int weekCountOk=CommonUtil.o2int(result.get("weekCountOk"));
		int monthCount=CommonUtil.o2int(result.get("monthCount"));
		int monthCountOk=CommonUtil.o2int(result.get("monthCountOk"));
		/**
		 * 总任务数
		 */
		result.put("zTodayCount", todayCount+todayCountOk);
		result.put("zWeekCount", weekCount+weekCountOk);
		result.put("zMonthCount", monthCount+monthCountOk);
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/getCountByWorkAndPatrol") 
	public Object getCountByWorkAndPatrol(@Param("variable")Integer variable,@Param("jcBdzId")Integer jcBdzId){
		
		Map<String, Object> param=new HashMap();
		param.put("jcBdzId", jcBdzId);
		param.put("variable", variable);
		Map<String, Object> result=kshzsService.getCountByWorkAndPatrol(param);
		double numberNo=CommonUtil.o2double(result.get("numberNo"));
		double numberOk=CommonUtil.o2double(result.get("numberOk"));
		double number=0;
		if(numberOk !=0){
			number=numberOk/(numberNo+numberOk)*100;
		}
		
		StringBuilder series = new StringBuilder();
		series.append("<chart   basefontsize ='11.5' manageResize='1' origW='350' origH='200'  palette='2' bgAlpha='0' bgColor='FFFFFF' lowerLimit='0' upperLimit='100' numberSuffix='%' showBorder='0' basefontColor='FFFFDD' chartTopMargin='5' chartBottomMargin='5'"); 
		series.append("toolTipBgColor='009999' gaugeFillMix='{dark-10},{light-70},{dark-10}' gaugeFillRatio='3' pivotRadius='8' gaugeOuterRadius='120' gaugeInnerRadius='70%'  ");
		series.append("gaugeOriginX='175' gaugeOriginY='170' trendValueDistance='5' tickValueDistance='3' manageValueOverlapping='1' autoAlignTickValues='1'>");
		series.append("   <colorRange>");
		series.append("      <color minValue='0' maxValue='33' code='FF654F'/>");
		series.append("      <color minValue='33' maxValue='66' code='F6BD0F'/>");
		series.append("      <color minValue='66' maxValue='100' code='8BBA00'/>");
		series.append("   </colorRange>");
		series.append("   <dials>");
		series.append("      <dial value='"+Math.round(number)+"' rearExtension='10' baseWidth='10'/>");
		series.append("   </dials>");
		series.append("   <annotations>");
		series.append("      <annotationGroup id='Grp1' showBelow='1' showShadow='1'>");
		series.append("         <annotation type='rectangle' x='$chartStartX+5' y='$chartStartY+5' toX='$chartEndX-5' toY='$chartEndY-5' fillColor='009999,333333' showBorder='0' />");
		series.append("      </annotationGroup>");
		series.append("   </annotations>");
		series.append("   <styles>");
		series.append("      <definition>");
		series.append("         <style name='RectShadow' type='shadow' strength='3'/>");
		series.append("                 <style name='trendvaluefont' type='font' bold='1' borderColor='FFFFDD'/>");
		series.append("      </definition>");
		series.append("      <application>");
		series.append("         <apply toObject='Grp1' styles='RectShadow' />");
		series.append("                 <apply toObject='Trendvalues' styles='trendvaluefont' />");
		series.append("      </application>");
		series.append("   </styles>");
		series.append("</chart>");
		return series;
	}
	
    
	@ResponseBody
	@RequestMapping("/getTodayWcShu") 
	public Object getTodayWcShu(@Param("jcBdzId")Integer jcBdzId){
		Map<String, Object> param=new HashMap();
//		param.put("jcBdzId", jcBdzId);
		List<Map<String, Object>> result=kshzsService.getTodayWcShu(param);
			StringBuilder series = new StringBuilder();
			series.append("<chart xAxisName='变电站' yAxisName='完成数量' showValues='0' basefontsize='11.5' decimals='0' formatNumberScale='0' palette='4'>");
			for (int i = 0; i < result.size(); i++) {
				series.append("<set label='"+result.get(i).get("bdzName")+"' value='"+result.get(i).get("number")+"' />");
			}
			series.append("<trendlines>");
			series.append("<line startValue='750' displayValue='Average' color='009900' valueOnRight='1' />");
			series.append("</trendlines>");
			series.append("</chart>");
			
		return series;
	}
	
}
